//
//  UIView+SignInView.m
//  1gene
//
//  Created by 1jiyin on 16/5/25.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "UIView+SignInView.h"
#define SignInViewTag 11122
#import "AFNetWorking.h"

@implementation UIView (SignInView)

-(void)showSignIn:(BOOL)isShow
{
    if (isShow) {
        [self getSuperSignInHttpData];
    }
}


-(void)getSuperSignInHttpData
{
    AFHTTPRequestOperationManager * manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    
    [manager GET:[NSString stringWithFormat:@"%@%@",UserInfo,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"userInfo===%@",dic);
        if (dic) {
            NSString *lastSignInTime=[NSString stringWithFormat:@"%@",dic[@"last_sign"]];
             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
             [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
             NSDate *date1 = [NSDate date];
             NSDate *date2 = [formatter dateFromString:[NSString stringWithFormat:@"%@",lastSignInTime]];
             NSTimeInterval aTimer = [date2 timeIntervalSinceDate:date1];
             
             int hour = (int)(aTimer/3600);
             
             int days=hour/24;
            if (days==0) {
                [self createSignView:[NSString stringWithFormat:@"%@",dic[@"all_sign"]]];
            }else{
                
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];


}


-(void)createSignView:(NSString *)days
{
    NSInteger day=[days integerValue];
    UIView *view=(id)[self viewWithTag:SignInViewTag];
    if (view==nil) {
        view=[[UIView alloc]initWithFrame:self.bounds];
        view.tag=SignInViewTag;
        view.backgroundColor=[UIColor whiteColor];
        [self addSubview:view];
        
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.width*(250/375.0))];
        imgView.image=[UIImage imageNamed:@"signin1"];
        [view addSubview:imgView];
        
        UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imgView.frame)+20, self.frame.size.width, 25)];
        lab1.text=[NSString stringWithFormat:@"坚持关爱健康%ld天!",day+1];
        lab1.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
        lab1.textAlignment=NSTextAlignmentCenter;
        lab1.font=[UIFont systemFontOfSize:22];
        [view addSubview:lab1];
        
        NSArray *arr=@[@{@"content1":@"健康币+1",@"content2":@"今天"},@{@"content1":@"健康币+3",@"content2":@"7天"},@{@"content1":@"健康币+10",@"content2":@"1个月"},@{@"content1":@"健康币+50",@"content2":@"3个月"},@{@"content1":@"健康币+80",@"content2":@"6个月"},@{@"content1":@"健康币+120",@"content2":@"9个月"}];
        
        CGFloat width=(self.frame.size.width-30*4)/3.0;
        CGFloat height=width*(110/144.0);
        CGFloat img_w=width*(50/144.0);
        CGFloat img_h=height*(53/110.0);
        
        UIButton *sureBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame=CGRectMake((self.frame.size.width*(1-336/750.0))/2.0, CGRectGetMaxY(lab1.frame)+10+(height+30)*2, self.frame.size.width*(336/750.0), self.frame.size.width*(67/750.0));
        [sureBtn setBackgroundImage:[UIImage imageNamed:@"signin2"] forState:UIControlStateNormal];
        [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn.titleLabel.font=[UIFont systemFontOfSize:22];
        [sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:sureBtn];
        
        UILabel *attentionLab=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(sureBtn.frame)+10, self.frame.size.width, 20)];
        attentionLab.text=@"*  健康币可以用于抽奖";
        attentionLab.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
        attentionLab.textAlignment=NSTextAlignmentCenter;
        attentionLab.font=[UIFont systemFontOfSize:16];
        [view addSubview:attentionLab];
        
        
        
        
        
        for (int i=0; i<2; i++) {
            
            for (int j=0; j<3; j++) {
                UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(30+(width+30)*j, CGRectGetMaxY(lab1.frame)+10+(height+30)*i, width, height+30)];
                [view addSubview:view1];
                
                UIView *subView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
                [view1 addSubview:subView];
                
                UIImageView *imgView1=[[UIImageView alloc]initWithFrame:CGRectMake((width-img_w)/2.0, 5, img_w, img_h)];
                [subView addSubview:imgView1];
                
                UILabel *content1=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imgView1.frame), width, height-img_h-5)];
                content1.text=arr[i*2+j][@"content1"];
                content1.font=[UIFont systemFontOfSize:16];
                content1.textAlignment=NSTextAlignmentCenter;
                [subView addSubview:content1];
                
                UILabel *content2=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(subView.frame), width,28)];
                content2.text=arr[i*2+j][@"content2"];
                content2.font=[UIFont systemFontOfSize:22];
                content2.textAlignment=NSTextAlignmentCenter;
                [view1 addSubview:content2];
                
                
                
                subView.layer.borderColor=[UIColor colorWithRed:189/255.0 green:192/255.0 blue:191/255.0 alpha:1].CGColor;
                subView.layer.borderWidth=2.0;
                subView.layer.cornerRadius=5.0;
                subView.backgroundColor=[UIColor colorWithRed:189/255.0 green:192/255.0 blue:191/255.0 alpha:1];
                
                
                imgView1.image=[UIImage imageNamed:@"signin4"];
                content1.textColor=[UIColor whiteColor];
                content2.textColor=[UIColor colorWithRed:189/255.0 green:192/255.0 blue:191/255.0 alpha:1];
                
                
                if (i*3+j==0) {
                    subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                    subView.layer.borderWidth=2.0;
                    subView.layer.cornerRadius=5.0;
                    subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    
                    
                    imgView1.image=[UIImage imageNamed:@"signin3"];
                    content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                    content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                    
                }
                   if (day==6) {
                        if (i*3+j==1) {
                            subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                            subView.layer.borderWidth=2.0;
                            subView.layer.cornerRadius=5.0;
                            subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                            
                            
                            imgView1.image=[UIImage imageNamed:@"signin3"];
                            content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                            content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        }
                    
                    }
                    if (day==29) {
                        if (i*3+j==1||i*3+j==2) {
                            subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                            subView.layer.borderWidth=2.0;
                            subView.layer.cornerRadius=5.0;
                            subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                            
                            
                            imgView1.image=[UIImage imageNamed:@"signin3"];
                            content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                            content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        }
                    }
                    
                    if (day==89) {
                        if (i*3+j==1||i*3+j==2||i*3+j==3) {
                            subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                            subView.layer.borderWidth=2.0;
                            subView.layer.cornerRadius=5.0;
                            subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                            
                            
                            imgView1.image=[UIImage imageNamed:@"signin3"];
                            content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                            content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        }
                    }

                    if (day==179) {
                        if (i*3+j==1||i*3+j==2||i*3+j==3||i*3+j==4) {
                            subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                            subView.layer.borderWidth=2.0;
                            subView.layer.cornerRadius=5.0;
                            subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                            
                            
                            imgView1.image=[UIImage imageNamed:@"signin3"];
                            content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                            content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        }
                    }

                    
                    if (day==269) {
                        if (i*3+j==1||i*3+j==2||i*3+j==3||i*3+j==4||i*3+j==5) {
                            subView.layer.borderColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1].CGColor;
                            subView.layer.borderWidth=2.0;
                            subView.layer.cornerRadius=5.0;
                            subView.backgroundColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                            
                            
                            imgView1.image=[UIImage imageNamed:@"signin3"];
                            content1.textColor=[UIColor colorWithRed:243/255.0 green:214/255.0 blue:81/255.0 alpha:1];
                            content2.textColor=[UIColor colorWithRed:236/255.0 green:114/255.0 blue:92/255.0 alpha:1];
                        }
                    }
            }
            
            
        }
    }

}

-(void)sureBtnClick:(UIButton *)sender
{
    UIView *view=(id)[self viewWithTag:SignInViewTag];
    [view removeFromSuperview];
}


@end






































